// StuderendeImpl.cpp: implementation of the CStuderendeImpl class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"
#include "StuderendeImpl.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CStuderendeImpl::CStuderendeImpl()
{
	m_session = bstr_t("");
	m_init = bstr_t("");
	m_cpr = bstr_t("");
	m_fornavn = bstr_t("");
	m_efternavn = bstr_t("");
	m_titel = bstr_t("");
	m_skoleId = bstr_t("");
	m_skoleDato = bstr_t("");
	m_okDato = bstr_t("");
	m_bem = bstr_t("");
	m_uddan = bstr_t("");
	m_status = bstr_t("");
	m_ident = bstr_t("");
	m_bruger = bstr_t("");

	m_firstStatus = TRUE;
	m_firstIdent = TRUE;
	m_recordStatus = NULL;
	m_recordIdent = NULL;
}

CStuderendeImpl::~CStuderendeImpl()
{
	if (m_recordStatus)
		m_recordStatus->Close();
	if (m_recordIdent)
		m_recordIdent->Close();

	m_collAddr.RemoveAll();
}

bool CStuderendeImpl::SetInfo()
{
	_CommandPtr cmd;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_OPRET_STUDERENDE";

	try {			
		cmd.CreateInstance(__uuidof(Command));
		cmd->ActiveConnection = CDatabase::GetConnection();
		cmd->CommandText = sql;
		cmd->CommandType = adCmdStoredProc;

		cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		cmd->Parameters->Append( cmd->CreateParameter("init", adVarChar, adParamInput, 255, m_init) );
		cmd->Parameters->Append( cmd->CreateParameter("cpr", adVarChar, adParamInput, 50, m_cpr) );
		cmd->Parameters->Append( cmd->CreateParameter("fornavn", adVarChar, adParamInput, 255, m_fornavn) );
		cmd->Parameters->Append( cmd->CreateParameter("efternavn", adVarChar, adParamInput, 255, m_efternavn) );
		cmd->Parameters->Append( cmd->CreateParameter("titel", adVarChar, adParamInput, 255, m_titel) );
		cmd->Parameters->Append( cmd->CreateParameter("skoleId", adVarChar, adParamInput, 255, m_skoleId) );
		cmd->Parameters->Append( cmd->CreateParameter("skoleDato", adVarChar, adParamInput, 50, m_skoleDato) );
		cmd->Parameters->Append( cmd->CreateParameter("okDato", adVarChar, adParamInput, 50, m_okDato) );
		cmd->Parameters->Append( cmd->CreateParameter("bem", adVarChar, adParamInput, 255, m_bem) );
		cmd->Parameters->Append( cmd->CreateParameter("uddan", adVarChar, adParamInput, 255, m_uddan) );
		cmd->Parameters->Append( cmd->CreateParameter("status", adVarChar, adParamInput, 255, m_status) );
		cmd->Parameters->Append( cmd->CreateParameter("ident", adVarChar, adParamInput, 255, m_ident) );
		cmd->Parameters->Append( cmd->CreateParameter("bruger", adVarChar, adParamInput, 255, m_bruger) );

		recordset = cmd->Execute(NULL, NULL, adCmdStoredProc);		

		if (recordset->ADO_EOF)
			return FALSE;

		m_guid = recordset->Fields->GetItem("GUID")->Value;
			
		int size = m_collAddr.GetSize();
		for (int i = 0; i<size; i++) {
			CAdresseImpl * addr = (CAdresseImpl*)m_collAddr[i];
			addr->SetInfo(m_guid);			
		}		
	} catch (_com_error &e) {
		char err[512];
		strncpy(err, e.ErrorMessage(), 512);
		return FALSE;
	}

	return TRUE;
}

bstr_t CStuderendeImpl::get_BrugerNavn()
{
	return m_bruger.copy();
}

void CStuderendeImpl::put_BrugerNavn(bstr_t  newVal)
{
	m_bruger = newVal;
}

bstr_t CStuderendeImpl::get_Identifikation()
{
	return m_ident.copy();
}

void CStuderendeImpl::put_Identifikation(bstr_t  newVal)
{
	m_ident = newVal;
}

bstr_t CStuderendeImpl::get_Status()
{
	return m_status.copy();
}

void CStuderendeImpl::put_Status(bstr_t  newVal)
{
	m_status = newVal;
}

bstr_t CStuderendeImpl::get_CPRNr()
{
	return m_cpr.copy();
}

void CStuderendeImpl::put_CPRNr(bstr_t  newVal)
{
	m_cpr = newVal;
}

bstr_t CStuderendeImpl::get_OKDato()
{
	return m_okDato.copy();
}

void CStuderendeImpl::put_OKDato(bstr_t  newVal)
{
	m_okDato = newVal;
}

bstr_t CStuderendeImpl::get_SkoleDato()
{
	return m_skoleDato.copy();
}

void CStuderendeImpl::put_SkoleDato(bstr_t  newVal)
{
	m_skoleDato = newVal;
}

bstr_t CStuderendeImpl::get_SkoleId()
{
	return m_skoleId.copy();
}

void CStuderendeImpl::put_SkoleId(bstr_t  newVal)
{
	m_skoleId = newVal;
}

bstr_t CStuderendeImpl::get_Uddannelse()
{
	return m_uddan.copy();
}

void CStuderendeImpl::put_Uddannelse(bstr_t  newVal)
{
	m_uddan = newVal;
}

bstr_t CStuderendeImpl::get_Bemaerkning()
{
	return m_bem.copy();
}

bstr_t CStuderendeImpl::get_Init()
{
	return m_init.copy();
}

void CStuderendeImpl::put_Init(bstr_t  newVal)
{
	m_init = newVal;
}

void CStuderendeImpl::put_Bemaerkning(bstr_t  newVal)
{
	m_bem = newVal;
}

bstr_t CStuderendeImpl::get_Titel()
{
	return m_titel.copy();
}

void CStuderendeImpl::put_Titel(bstr_t  newVal)
{
	m_titel = newVal;
}

void CStuderendeImpl::put_Adresse(CComObject<CAdresse> * pAdresse)
{
	BSTR val;

	if (!pAdresse)
		return;

	if (2 <= m_collAddr.GetSize())
		return;

	pAdresse->put_GUID(m_guid);

	CAdresseImpl * implH = new CAdresseImpl;
	implH->put_GUID(m_guid);

	pAdresse->get_AdresseType(&val);
	implH->put_AdresseType(val);
	pAdresse->get_PostNummer(&val);
	implH->put_PostNr(val);
	pAdresse->get_Email(&val);
	implH->put_Email(val);
	pAdresse->get_Gade(&val);
	implH->put_Gade(val);
	pAdresse->get_Telefax(&val);
	implH->put_Telefax(val);
	pAdresse->get_Telefon(&val);
	implH->put_Telefon(val);	

	m_collAddr.Add(implH);
}

bstr_t CStuderendeImpl::get_Efternavn()
{
	return m_efternavn.copy();
}

void CStuderendeImpl::put_Efternavn(bstr_t  newVal)
{
	m_efternavn = newVal;
}

bstr_t CStuderendeImpl::get_Fornavn()
{
	return m_fornavn.copy();
}

void CStuderendeImpl::put_Fornavn(bstr_t  newVal)
{
	m_fornavn = newVal;
}

bool CStuderendeImpl::HentStatus(bool toStart, bstr_t * guid, bstr_t * status)
{
	_CommandPtr cmd;
	_RecordsetPtr recordset;

	bstr_t g, s;
	const char sql[255] = "EBAS_SP_HENT_STATUS_ALLE";

	try {		
		if (m_firstStatus || toStart) {
			cmd.CreateInstance(__uuidof(Command));
			cmd->ActiveConnection = CDatabase::GetConnection();
			cmd->CommandText = sql;
			cmd->CommandType = adCmdStoredProc;

			cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );

			m_recordStatus = cmd->Execute(NULL, NULL, adCmdStoredProc);		
		}

		if (m_recordStatus->ADO_EOF) 
			return FALSE;

		if (!m_firstStatus) {
			m_firstStatus = FALSE;
			m_recordStatus->MoveNext();
		}

		g = m_recordStatus->Fields->GetItem("GUID")->Value;
		s = m_recordStatus->Fields->GetItem("STATUS")->Value;

		*guid = g.copy();
		*status = s.copy();
	} catch (_com_error &e) {
		char err[512];
		strncpy(err, e.Description(), 512);
		return FALSE;
	}

	return TRUE;
}

bool CStuderendeImpl::HentIdentifikation(bool toStart, bstr_t * guid, bstr_t * identifikation)
{
	_CommandPtr cmd;

	const char sql[255] = "EBAS_SP_HENT_IDENTIFIKATION_ALLE";
	bstr_t g, i;

	try {
		if (m_firstIdent || toStart) {
			cmd.CreateInstance(__uuidof(Command));
			cmd->ActiveConnection = CDatabase::GetConnection();
			cmd->CommandText = sql;
			cmd->CommandType = adCmdStoredProc;

			cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );

			m_recordIdent = cmd->Execute(NULL, NULL, adCmdStoredProc);		
		}
			
		if (m_recordIdent->ADO_EOF) 
			return FALSE;

		if (!m_firstStatus) {
			m_firstIdent = FALSE;
			m_recordIdent->MoveNext();
		}

		g = m_recordIdent->Fields->GetItem("GUID")->Value;
		i = m_recordIdent->Fields->GetItem("IDENTIFIKATION")->Value;

		*guid = g.copy();
		*identifikation = i.copy();
	} catch (_com_error &e) {
		char err[512];
		strncpy(err, e.ErrorMessage(), 512);
		return FALSE;
	}

	return TRUE;
}
